<?php
/**
 * Object Relational Persistence Mapping Class for table: service_group
 *
 * @package	com.clearhealth
 * @author	ClearHealth Inc.
 */
class ServiceGroup extends ORDataObject {

	/**#@+
	 * Fields of table: service_group mapped to class members
	 */
	var $service_group_id		= 	'';
	var $vi_name				= 	'';
	var $en_name				= 	'';
	var $order					=	'';
	/**#@-*/


	/**
	 * DB Table
	 */
	var $_table = 'service_group';

	/**
	 * Primary Key
	 */
	var $_key = 'service_group_id';
	
	/**
	 * Internal Name
	 */
	var $_internalName = 'ServiceGroup';

	/**
	 * Handle instantiation
	 */
	function ServiceGroup() {
		parent::ORDataObject();
	}
	/**
	 * Get all service groups.
	 *
	 * @return List of service groups
	 */
	public function listAll() {
		$field	=	L10nHelper::getLocale();
		$field	=	L10nHelper::changeField($field, "name");
		$ds = new Datasource_sql();
        $sql = " SELECT service_group_id , $field as name, 'order'
                 FROM $this->_table
                 ORDER BY $field";
		$res = $this->_execute($sql);
		return $res;
	}
	
	
	/**
	 * Get all service groups combo.
	 *
	 * @return List of service groups
	 */
	public function getListAllForCombo() {
		$field	=	L10nHelper::getLocale();
		$field	=	L10nHelper::changeField($field,"name");
		$ds = new Datasource_sql();
		$sql = array (
			'cols' 		=> 	"$this->_key, $field as name",
			'from'		=> 	"$this->_table",
			'orderby'	=>	"name");
		$ds->setup($this->_db, $sql);
		return $ds->toArray("$this->_key", 'name');
	}
	
	function checkName($name, $serviceGroupId) {
		$field	=	L10nHelper::getLocale();
		$field	=	L10nHelper::changeField($field, "name");
		$sql = "SELECT $field name from $this->_table where $field = '$name'";

		if($serviceGroupId != "" ){
			 $sql .= " AND service_group_id != $serviceGroupId";
		}
		$res = $this->_execute($sql);
		// duplicated name
		if ($res && !$res->EOF) {
			return 0;
		}
		return 1;
	}
	
	public function getGroupList(){
		$field	=	L10nHelper::getLocale();
		$field	=	L10nHelper::changeField($field, "name");
				
		$ds		=	&new Datasource_sql();
		$sql	=	array(
			'cols'		=>	"$this->_key as id, $field name",
			'from'		=>	"$this->_table",
			'orderby'	=>	"name"
		);
		$ds->setup($this->_db,$sql);
		return $ds->toArray('id','name');
	}

}
?>
